package dictcn.com.activity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ActivityGroup;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import dictcn.com.R;
import dictcn.com.control.MyDatabaseHelper;
import dictcn.com.control.MytabOperate;
import dictcn.com.model.BookName;

public class WordList extends ActivityGroup {//生词本界面activity

 private ImageButton imgBtnTongbuj,imgBtnEdit2j;
 private ListView listView=null;
 private ImageButton imgbtnAddWordList=null;
 private SQLiteOpenHelper helper=null;
 private MytabOperate mytab=null;
 private List<BookName> bookNameList=null;
 private SimpleAdapter simpleAdapter=null;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.world_list);
    }
	
	@Override
	protected void onStart() {
		
        //在onstart里面设布局控件的显示，为以后activity恢复的时候也能刷新控件做准备
        setupViewComponent();
		super.onStart();
	}
	
	private void setupViewComponent(){
		  imgBtnTongbuj=(ImageButton)findViewById(R.id.imgBtnTongbu);
		  imgBtnEdit2j=(ImageButton)findViewById(R.id.imgBtnEdit2);	
		  imgbtnAddWordList=(ImageButton)findViewById(R.id.btnAddWordList);
		  listView=(ListView)findViewById(R.id.lv_list_words);
		  
		  setBookNameData();
	      
		  //添加生词本按钮监听事件
	      imgbtnAddWordList.setOnClickListener(new OnClickListener() {
			
		  public void onClick(View v) {
				// TODO Auto-generated method stub
			  //自定义对话框的加载显示
				LayoutInflater inflater=LayoutInflater.from(WordList.this);
				View view=inflater.inflate(R.layout.word_list_dlg, null);
				final EditText edit=(EditText)view.findViewById(R.id.edit_wordlist_content);
				Dialog dlg=new AlertDialog.Builder(WordList.this)
							.setView(view)
							.setMessage("添加生词本-名称")
							.setPositiveButton("确定", new AlertDialog.OnClickListener() {
								
								public void onClick(DialogInterface dialog, int which) {
									// TODO Auto-generated method stub
									String editContent=edit.getText().toString().trim();
									
									if(editContent.equals("")){
										Toast.makeText(WordList.this, "生词本内容不能为空", Toast.LENGTH_LONG).show();
									}else{
										//创建新的生词本语句！！！
											helper=new MyDatabaseHelper(WordList.this);
										    helper.getWritableDatabase();
											mytab=new MytabOperate(
											    	helper.getWritableDatabase());
											//返回创建的生词本时候已经存在信息
											boolean flag=mytab.createBookName(editContent);					
											if(flag){
												Toast.makeText(WordList.this, "生词本创建成功", Toast.LENGTH_SHORT).show();
												setBookNameData();
												
											}else{
												Toast.makeText(WordList.this, "生词本名称已存在", Toast.LENGTH_SHORT).show();
												edit.setText("");
											}
									}
								}
							})
							.setNegativeButton("取消", null)
							.create();
				dlg.show();
			}
		});
	      
	    //编辑按钮监听事件，跳转Activity
	    imgBtnEdit2j.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent=new Intent();
				intent.setClass(WordList.this, DeleteBookName.class);
				Bundle bundle=new Bundle();
				
				//传递信息为生词本列表集合
				bundle.putSerializable("wordList", (Serializable) bookNameList);
				intent.putExtra("boundle", bundle);
				startActivity(intent);
			}
		});
	    
	    //listView中生词本点击相应事件
	    listView.setOnItemClickListener(new OnItemClickListener() {
	    	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
	    			long arg3) {
	    		// TODO Auto-generated method stub
	    		if(bookNameList.get(arg2).getCount()==0)
	    		{
	    			Toast.makeText(WordList.this, "生词本中没有单词！", Toast.LENGTH_SHORT).show();
	    		}else
	    		{
	    			//显示生词本中所有能生词的基本信息
	    			Intent intent=new Intent();
	    			intent.putExtra("b_id", bookNameList.get(arg2).getB_id());
	    			intent.setClass(WordList.this, BookNameListShow.class);
	    			startActivity(intent);
	    		}    		
	    	}
		});
	       	        
	}
	
	//设置listView中的内同及控件中数据的加载更新
	public void setBookNameData(){
		helper=new MyDatabaseHelper(WordList.this);
	    helper.getWritableDatabase();
		mytab=new MytabOperate(
		    	helper.getWritableDatabase());
		bookNameList=new ArrayList<BookName>();
		bookNameList=mytab.SelctAllBookName();
		ArrayList<Map<String,String>> listItem=new ArrayList<Map<String,String>>();
		  Map<String,String> map;
		  for(int i=0;i<bookNameList.size();i++){
			  map=new HashMap<String,String>();
			  map.put("BookName", bookNameList.get(i).getB_name());
			  map.put("BookNameCount", "生词数： "+bookNameList.get(i).getCount());
			  listItem.add(map);
		  }	         
	      simpleAdapter=new SimpleAdapter(WordList.this,
	        		listItem,
	        		R.layout.word_list_item,
	        		new String[]{"BookName","BookNameCount"},
	        		new int[]{R.id.txtWordName,R.id.txtWordCount}
	     );         
	    listView.setAdapter(simpleAdapter);
	}
	
	//返回键监听
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode==KeyEvent.KEYCODE_BACK)
		{
			WordList.this.finish();
		}
		return super.onKeyDown(keyCode, event);
	}
}
